package com.zzh.myapplication.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import net.icarplus.car.R;
import net.icarplus.car.tools.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/21.
 */
public class GuidePageActivity extends Activity {
    private ViewPager vp_container;
    private List<ImageView> viewlist;
    private LinearLayout ll_ptgroup;
    private View v_pt_red;
    private Button btn_startPlay;

    private int distlength;// 2个小圆点之间的距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide_page);
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        ll_ptgroup = (LinearLayout) findViewById(R.id.ll_ptgroup);
        v_pt_red = findViewById(R.id.v_pt_red);
        btn_startPlay = (Button) findViewById(R.id.btn_star_try);
        //初始化数据
        initData();
        vp_container = (ViewPager) findViewById(R.id.vp_container);
        vp_container.setAdapter(new guideViewPagerAdatpter());
        vp_container.setOnPageChangeListener(new myPageChangeListener());

    }

    /**
     * 小红点 滑动动画
     * 1 获取小2点之间的间隔
     * pt0.getleft - pt1.getleft
     * 何时获取？ ----> 在measure之后才能获取
     * ---> 可在Layout中获取
     */
    private class myPageChangeListener implements ViewPager.OnPageChangeListener {
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            // 当ViewPager在滑动过程中 动态计算出小红点的的位移值
            int marginLeft = (int) ((position + positionOffset) * distlength);
            //改变红色小球的位置
            android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v_pt_red.getLayoutParams();
            params.leftMargin = marginLeft;//设置小红点相对自己初始位置的偏移量
            v_pt_red.setLayoutParams(params);
        }

        @Override
        public void onPageSelected(int position) {
            //判断当前选中page 是否是最后一页
            if (position == viewlist.size() - 1) {
                btn_startPlay.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void initData() {
        int[] picIds = {R.mipmap.ui1, R.mipmap.ui2, R.mipmap.third};
        viewlist = new ArrayList<ImageView>();
        ImageView iv;
        View pt;
        for (int i = 0; i < picIds.length; i++) {
            iv = new ImageView(GuidePageActivity.this);
            iv.setBackgroundResource(picIds[i]);
            viewlist.add(iv);
            // 往线性布局中添加点
            //ll_ptgroup
            pt = new View(this);
            pt.setBackgroundResource(R.drawable.pt_shap_normal);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            if (i != 0) {
                params.leftMargin = 20;
            }
            pt.setLayoutParams(params);
            ll_ptgroup.addView(pt);
        }
        // 设置view树layout监听器
        v_pt_red.getViewTreeObserver().addOnGlobalLayoutListener(new myOnGlobalLayOutListener());
    }

    // 会递归监听整个view树的layout事件：所以在测到2点之间的距离后 取消监听
    private class myOnGlobalLayOutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            // 当进入这个方法时：整个view树都布局完成了 可以调用view.getlef()了
            distlength = ll_ptgroup.getChildAt(1).getLeft() - ll_ptgroup.getChildAt(0).getLeft();
            // 当测到2点之间的距离之后 取消监听
            v_pt_red.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }

    // viewpager 的适配器
    private class guideViewPagerAdatpter extends PagerAdapter {
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewlist.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            // 将view添加到Viewpager中
            View v = viewlist.get(position);
            container.addView(v);
            // 将view 对象返回
            return v;
        }
    }
    public void gotoMain(View v) {
        // 在配置文件中设置 以完成引导
        SPUtils.saveBoolean(GuidePageActivity.this,"frist",true);
        Intent intent = new Intent(GuidePageActivity.this, MainActivity.class);
        // 跳转到主界面
        startActivity(intent);
        // 关闭本界面
        finish();
    }

}
